package com.lkd.handler;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.base.Strings;
import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.TaskCompleteContract;
import com.lkd.contract.VendoutContract;
import com.lkd.emq.Topic;
import com.lkd.entity.VendingMachineEntity;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author Administrator
 */
@Component
@Topic(TopicConfig.VMS_COMPLETED_TOPIC)
public class CompletedMsgHandler implements MsgHandler {

    @Autowired
    private VendingMachineService vmService;
    /**
     * 售货机微服务接收工单微服务发送的消息
     * @param jsonMsg  消息内容
     * @throws IOException
     */

    @Override
    public void process(String jsonMsg) throws IOException {
        //将JsonMsg消息进行类型转换成完成工单协议类型
        TaskCompleteContract contract= JsonUtil.getByJson(jsonMsg, TaskCompleteContract.class );
//        LambdaQueryWrapper<VendingMachineEntity>wrapper =new LambdaQueryWrapper<>();
//        wrapper.eq(VendingMachineEntity::getInnerCode,contract.getInnerCode());
//        VendingMachineEntity entity =vmService.getOne(wrapper);
        //健壮性判断
        if(contract==null || Strings.isNullOrEmpty(contract.getInnerCode())  ) {
            return;
        }
        //如果是投放工单，将售货机修改为运营状态
        if( contract.getTaskType()== VMSystem.TASK_TYPE_DEPLOY){
            vmService.updateStatus(  contract.getInnerCode(), VMSystem.VM_STATUS_RUNNING   );
        }
        //如果是撤机工单，将售货机修改为撤机状态
        if( contract.getTaskType()== VMSystem.TASK_TYPE_REVOKE){
            vmService.updateStatus(  contract.getInnerCode(), VMSystem.VM_STATUS_REVOKE  );
            //vmService.updateById(entity);
        }
    }
}
